package com.qinge.blog.security;

import com.alibaba.fastjson.JSON;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.qinge.blog.entity.BlogUser;
import com.qinge.blog.service.BlogUserService;
import com.qinge.blog.utils.jwt.JwtUtil;
import com.qinge.blog.utils.redis.RedisUtil;
import com.qinge.blog.utils.result.JsonResult;
import com.qinge.blog.utils.result.ResultTool;
import io.netty.util.internal.StringUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author QDW
 * @date 2022/4/29 12:13
 * @description CustomizeAuthenticationSuccessHandler
 **/
@Component
public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler {

    @Resource
    BlogUserService blogUserService;

    @Resource
    private RedisUtil redisUtil;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, IOException {
        // 更新用户表上次登录时间、更新人、更新时间等字段

        // User userDetails = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        BlogUser user = (BlogUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        // 更新城市等信息
        if(!StringUtils.isBlank(httpServletRequest.getParameter("loginProvince"))){
            user.setLoginProvince(httpServletRequest.getParameter("loginProvince"));
        }
        if(!StringUtils.isBlank(httpServletRequest.getParameter("loginCity"))){
            user.setLoginCity(httpServletRequest.getParameter("loginCity"));
        }
        if(!StringUtils.isBlank(httpServletRequest.getParameter("loginLongitude"))){
            user.setLoginLongitude(httpServletRequest.getParameter("loginLongitude"));
        }
        if(!StringUtils.isBlank(httpServletRequest.getParameter("loginLatitude"))){
             user.setLoginLatitude(httpServletRequest.getParameter("loginLatitude"));
        }

        blogUserService.upsert(user);

        // 此处还可以进行一些处理，比如登录成功之后可能需要返回给前台当前用户有哪些菜单权限，
        // 进而前台动态的控制菜单的显示等，具体根据自己的业务需求进行扩展

        String javaWebToken = JwtUtil.createJWT(user.getId().toString());

        Map<String, Object> map = new HashMap<>();
        map.put("token", javaWebToken);
        map.put("user",user);

        // 把完整的用户信息存入redis  userid作为key
        redisUtil.setCacheObject("login:" + user.getId(), user);

        // 返回json数据
        JsonResult result = ResultTool.success(map);
        // 处理编码方式，防止中文乱码的情况
        httpServletResponse.setContentType("text/json;charset=utf-8");
        // 塞到HttpServletResponse中返回给前台
        httpServletResponse.getWriter().write(JSON.toJSONString(result));
    }
}
